package com.netkiller.mailmonitor.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailService.Message;
import com.google.appengine.api.mail.MailServiceFactory;
import com.netkiller.mailmonitor.dao.AppUserDao;
import com.netkiller.mailmonitor.entity.AppUser;
import com.netkiller.mailmonitor.util.UserUtil;
import com.netkiller.mailmonitor.vo.AppProperties;
import com.netkiller.mailmonitor.vo.DomainConfig;

/**
 * Service for app user
 * @author JKB
 *
 */
@Service
public class AppUserService {

	
	@Autowired
	private AppProperties appProperties;
	
	@Autowired AppUserDao appUserDao;
	
	@Autowired
	DomainConfig domainConfig;
	
	/**
	 * save method
	 * @param appUser
	 * @return
	 */
	public AppUser save(AppUser appUser) {
		AppUser obj = null;
		if (!StringUtils.isBlank(appUser.getEmail())) {
			if (StringUtils.isBlank(appUser.getDomainName())) {
				appUser.setDomainName(UserUtil.getDomain(appUser.getEmail()));
			}
			obj = appUserDao.create(appUser);
		}
		return obj;
	}
	
	/**
	 * fetch method
	 * @param email
	 * @return
	 */
	public AppUser getUser(String email) {
		return appUserDao.get(email);
	}
	
	/**
	 * fetch method
	 * @param key
	 * @return
	 */
	public AppUser getUser(Key key) {
		return appUserDao.get(AppUser.class	,key);
	}

	/**
	 * fetch all method
	 * @return
	 */
	public List<AppUser> getAllUsers() {
		return appUserDao.getAll();
	}
	
	/**
	 * save new token in appuser entity
	 * @param email
	 * @param accessSecret
	 * @return
	 */
	public AppUser updateUserTokenSecret(String email, String accessSecret) {
		AppUser appUser = getUser(email);
		if(appUser==null){
			appUser = new AppUser();
			appUser.setEmail(email);			
		}
		appUser.setOauthtokensecret(accessSecret);
		return save(appUser);		
	}
	
	/**
	 * save new token in appuser entity
	 * @param appUser
	 * @param accessToken
	 * @return
	 */
	public AppUser updateUserToken(AppUser appUser, String accessToken) {
		appUser.setOauthtoken(accessToken);
		return save(appUser);		
	}


	
	/**
	 * method to send authorization mail
	 * @param senderEMail
	 * @param adminEmail
	 * @param email
	 * @param approvalUrl
	 */
	public void sendApprovalMailToUser(String senderEMail,String adminEmail, String email, String approvalUrl) {
		Message message = new Message();
		message.setTo(email);
		message.setSubject("Your Domain Admin has requested authorization for "
				+ appProperties.getApplicationName());
		String body = "Hello,<br> " + adminEmail + " has requested your approval for " + appProperties.getApplicationName() +
		".<br><br> Please visit the link below and grant approval.<br><br>You must be logged in with your " + UserUtil.getDomain(email) +
		" account. <br><br><b>Please log out of all other accounts first.</b><br><br> And then click on the following link, or copy/paste in your browser : " + approvalUrl +
		"<br><br> " +
		"Please ensure that IMAP is enabled under settings on your " + UserUtil.getDomain(email) +
		" account." +
		"<br><br> Thanks and regards, <br>Email Statistics Team.";
		message.setHtmlBody(body);
		message.setSender(senderEMail);
		MailService service = MailServiceFactory.getMailService();
		try {
			service.send(message);
			System.out.println("sending mail to " + email);
		} catch (Exception e) {
			System.out.println("sending mail FAILED for " + email + e.getMessage() );
			e.printStackTrace();
		}

	}

	/**
	 * check if user is admin
	 * @param email
	 * @return
	 */
	public boolean isAdmin(String email){
		boolean returnValue = false;
		if(domainConfig.getUsers().contains(email)){
			returnValue = true;
		}
		return returnValue;
	}


}
